<?php
class ModelBlogPost extends Model {
	public function addPost($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts SET sort_order = '" . (int)$this->request->post['sort_order'] . "', published = '" . (int)$data['published'] . "',  created_by=" . (int)$data['created_by'] . ", modified_by=" . (int)$data['modified_by'] . ", created=NOW(), modified=NOW()");

		$id = $this->db->getLastId(); 
			
		foreach ($data['description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_description SET blog_post_id= '" . (int)$id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		if (isset($data['post_store'])) {
			foreach ($data['post_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_store SET blog_post_id = '" . (int)$id . "', store_id = '" . (int)$store_id . "'");
			}
		}

		if (isset($data['post_layout'])) {
			foreach ($data['post_layout'] as $store_id => $layout) {
				if ($layout) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_layout SET blog_post_id = '" . (int)$id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		if (isset($data['post_category'])) {
			foreach ($data['post_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_blog_categories SET blog_post_id = '" . (int)$id . "', blog_category_id = '" . (int)$category_id . "'");
				
				$this->incrementPostCount($category_id);
			}		
		}		
				
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'id=" . (int)$id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('blog_post');
	}
	
	public function editPost($id, $data) {
		
		$query = $this->db->query("SELECT blog_category_id FROM " . DB_PREFIX . "blog_posts_to_blog_categories WHERE blog_post_id = '" . (int)$id . "'");

		foreach ($query->rows as $result) {
			$this->decrementPostCount($result['blog_category_id']);
		}
		
		
		$this->db->query("UPDATE " . DB_PREFIX . "blog_posts SET sort_order = '" . (int)$data['sort_order'] . "', published = '" . (int)$data['published'] . "', modified=NOW(), modified_by=" . (int)$data['modified_by'] . " WHERE id = '" . (int)$id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_description WHERE blog_post_id = '" . (int)$id . "'");
					
		foreach ($data['description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_description SET blog_post_id = '" . (int)$id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_to_store WHERE blog_post_id = '" . (int)$id . "'");
		
		if (isset($data['post_store'])) {
			foreach ($data['post_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_store SET blog_post_id = '" . (int)$id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_to_layout WHERE blog_post_id = '" . (int)$id . "'");

		if (isset($data['post_layout'])) {
			foreach ($data['post_layout'] as $store_id => $layout) {
				if ($layout['layout_id']) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_layout SET blog_post_id = '" . (int)$id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
		
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_to_blog_categories WHERE blog_post_id = '" . (int)$id . "'");
		
		if (isset($data['post_category'])) {
			foreach ($data['post_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "blog_posts_to_blog_categories SET blog_post_id = '" . (int)$id . "', blog_category_id = '" . (int)$category_id . "'");
				
				$this->incrementPostCount($category_id);
			}		
			
		}
				
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'id=" . (int)$id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'id=" . (int)$id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
	}
	
	
	public function publishPost($id) {
		$this->db->query("UPDATE blog_posts SET published='1' WHERE id = '" . (int)$id . "'");
		$this->cache->delete('blog_post');
	}	
	
	public function unPublishPost($id) {
		$this->db->query("UPDATE blog_posts SET published='0' WHERE id = '" . (int)$id . "'");

		$this->cache->delete('blog_post');
	}	
	
	public function deletePost($id) {
		
		$query = $this->db->query("SELECT blog_category_id FROM " . DB_PREFIX . "blog_posts_to_blog_categories WHERE blog_post_id = '" . (int)$id . "'");

		foreach ($query->rows as $result) {
			$this->decrementPostCount($result['blog_category_id']);
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts WHERE id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_description WHERE blog_post_id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_to_store WHERE blog_post_id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_posts_to_layout WHERE blog_post_id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'blog_post_id=" . (int)$id . "'");

		$this->cache->delete('blog_post');
	}	

	public function getPost($id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'id=" . (int)$id . "') AS keyword FROM " . DB_PREFIX . "blog_posts WHERE id = '" . (int)$id . "'");
		
		return $query->row;
	}
		
	public function getPosts($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "blog_posts bp LEFT JOIN " . DB_PREFIX . "blog_posts_description bpd ON (bp.id = bpd.blog_post_id) WHERE bpd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
			if (!empty($data['filter_title'])) {
				$sql .= "  AND LCASE(bpd.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
			}

			if (isset($data['filter_order']) && !is_null($data['filter_order'])) {
				$sql .= " AND bp.sort_order = '" . (int)$data['filter_order'] . "'";
			}
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND bp.published = '" . (int)$data['filter_status'] . "'";
			}
		
			$sort_data = array(
				'bpd.title',
				'bp.sort_order',
				'bp.published',
				'bp.created',
				'bp.modified'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY bp.modified";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$post_data = $this->cache->get('blog_post.' . (int)$this->config->get('config_language_id'));
		
			if (!$post_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "blog_posts bp LEFT JOIN " . DB_PREFIX . "blog_posts_description bpd ON (bp.id = bpd.blog_post_id)  WHERE bpd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY bpd.title");
	
				$post_data = $query->rows;
			
				$this->cache->set('blog_post.' . (int)$this->config->get('config_language_id'), $post_data);
			}	
	
			return $post_data;			
		}
	}
	
	public function getPostDescriptions($id) {
		$post_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "blog_posts_description WHERE blog_post_id = '" . (int)$id . "'");

		foreach ($query->rows as $result) {
			$post_description_data[$result['language_id']] = array(
				'title'       => $result['title'],
				'description' => $result['description']
			);
		}
		
		return $post_description_data;
	}
	
	public function getPostStores($id) {
		$post_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "blog_posts_to_store WHERE blog_post_id = '" . (int)$id . "'");

		foreach ($query->rows as $result) {
			$post_store_data[] = $result['store_id'];
		}
		
		return $post_store_data;
	}

	public function getPostLayouts($id) {
		$post_layout_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "blog_posts_to_layout WHERE blog_post_id = '" . (int)$id . "'");
		
		foreach ($query->rows as $result) {
			$post_layout_data[$result['store_id']] = $result['layout_id'];
		}
		
		return $post_layout_data;
	}
		
	public function getTotalPosts($data) {
      	$sql = "";

		if (!empty($data['filter_title'])) {
			$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_posts as bp LEFT JOIN " . DB_PREFIX . "blog_posts_description as bpd ON(bpd.blog_post_id = bp.id) WHERE  bpd.language_id = '" . (int)$this->config->get('config_language_id')."'";
		}else{
			$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_posts as bp WHERE 1";
		}

		if (!empty($data['filter_title'])) {
			$sql .= " AND LCASE(bpd.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}

		if (isset($data['filter_order']) && !is_null($data['filter_order'])) {
			$sql .= " AND bp.sort_order = '" . (int)$data['filter_order'] . "'";
		}
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND bp.published = '" . (int)$data['filter_status'] . "'";
		}
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}	
	
	public function getTotalPostsByLayoutId($layout_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_posts_to_layout WHERE layout_id = '" . (int)$layout_id . "'");

		return $query->row['total'];
	}	
	
	public function getPostCategories($post_id) {
		$post_category_data = array();
		
		$query = $this->db->query("SELECT blog_category_id FROM " . DB_PREFIX . "blog_posts_to_blog_categories WHERE blog_post_id = '" . (int)$post_id . "'");
		
		foreach ($query->rows as $result) {
			$post_category_data[] = $result['blog_category_id'];
		}

		return $post_category_data;
	}

	function decrementPostCount($catId){
		$this->db->query('UPDATE `'.DB_PREFIX.'blog_categories` SET post_count = post_count-1 WHERE `id`='.$catId.' LIMIT 1');
	}
	
	function incrementPostCount($catId){
		$this->db->query('UPDATE `'.DB_PREFIX.'blog_categories` SET post_count = post_count+1 WHERE `id`='.$catId.' LIMIT 1');
	}
}
?>